package com.mc.read.adminapi.service;

import com.alibaba.fastjson.JSON;
import com.mc.read.adminapi.Aspect.UserLog;
import com.mc.read.adminapi.Aspect.UserPer;
import com.mc.read.adminapi.manager.UserManager;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuhao
 * @date 2019/6/12 15:26
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    UserManager userManager;


    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工列表展示 selectUserList
     **/
    @UserPer(permission = "user:user:select")
    @UserLog(operateName = "员工列表展示", type = 4)
    public BaseRespose selectUserList(BaseRequest baseRequest) {
        log.info("员工列表展示,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectUserList(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("员工列表展示 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("员工列表展示 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加员工 addUser
     **/
    @UserPer(permission = "user:user:add")
    @UserLog(operateName = "添加员工", type = 1)
    public BaseRespose addUser(BaseRequest baseRequest) {
        log.info("添加员工,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.addUser(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("添加员工 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加员工 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改员工信息 updateUserInfo
     **/
    @UserPer(permission = "user:user:update")
    @UserLog(operateName = "修改员工信息", type = 3)
    public BaseRespose updateUserInfo(BaseRequest baseRequest) {
        log.info("修改员工信息,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updateUserInfo(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("修改员工信息 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改员工信息 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除员工 deleteUser
     **/
    @UserPer(permission = "user:user:del")
    @UserLog(operateName = "删除员工", type = 2)
    public BaseRespose deleteUser(BaseRequest baseRequest) {
        log.info("删除员工,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.deleteUser(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("删除员工 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除员工 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工修改密码 updateUserPsd
     **/
//    @UserLog(operateName = "员工修改密码", type = 3)
    public BaseRespose updateUserPsd(BaseRequest baseRequest) {
        log.info("员工修改密码,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updateUserPsd(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("员工修改密码 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("员工修改密码 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 角色列表展示 selectRoleList
     **/
    @UserPer(permission = "user:role:select")
    @UserLog(operateName = "角色列表展示", type = 4)
    public BaseRespose selectRoleList(BaseRequest baseRequest) {
        log.info("角色列表展示,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectRoleList(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("角色列表展示 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("角色列表展示 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加角色 addRole
     **/
    @UserPer(permission = "user:role:add")
    @UserLog(operateName = "添加角色", type = 1)
    public BaseRespose addRole(BaseRequest baseRequest) {
        log.info("添加角色,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.addRole(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("添加角色 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加角色 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改角色 updateRole
     **/
    @UserPer(permission = "user:role:update")
    @UserLog(operateName = "修改角色", type = 3)
    public BaseRespose updateRole(BaseRequest baseRequest) {
        log.info("修改角色,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updateRole(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("修改角色 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改角色 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除角色 deleteRole
     **/
    @UserPer(permission = "user:role:del")
    @UserLog(operateName = "删除角色", type = 2)
    public BaseRespose deleteRole(BaseRequest baseRequest) {
        log.info("删除角色,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.deleteRole(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("删除角色 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除角色 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询员工角色 selectUserRole
     **/
    @UserPer(permission = "user:user:selectUserRole")
    @UserLog(operateName = "查询员工角色", type = 4)
    public BaseRespose selectUserRole(BaseRequest baseRequest) {
        log.info("查询员工角色,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectUserRole(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("查询员工角色 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询员工角色 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 设置员工角色 updateUserRole
     **/
    @UserPer(permission = "user:user:updateUserRole")
    @UserLog(operateName = "设置员工角色", type = 3)
    public BaseRespose updateUserRole(BaseRequest baseRequest) {
        log.info("设置员工角色,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updateUserRole(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("设置员工角色 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("设置员工角色 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询角色权限 selectRolePer
     **/
    @UserPer(permission = "user:role:selectUserPer")
    @UserLog(operateName = "查询角色权限", type = 4)
    public BaseRespose selectRolePer(BaseRequest baseRequest) {
        log.info("查询角色权限,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectRolePer(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("查询角色权限 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询角色权限 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 设置角色权限 updateUserPer
     **/
    @UserPer(permission = "user:role:updateRolePer")
    @UserLog(operateName = "设置角色权限", type = 3)
    public BaseRespose updateUserPer(BaseRequest baseRequest) {
        log.info("设置角色权限,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updateUserPer(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("设置角色权限 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("设置角色权限 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询权限列表 selectPerList
     **/
    public BaseRespose selectPerList(BaseRequest baseRequest) {
        log.info("查询权限列表,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectPerList(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("查询权限列表 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询权限列表 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询权限详情 selectPerInfo
     **/
    public BaseRespose selectPerInfo(BaseRequest baseRequest) {
        log.info("查询权限详情,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectPerInfo(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("查询权限详情 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询权限详情 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加权限 addPer
     **/
    public BaseRespose addPer(BaseRequest baseRequest) {
        log.info("添加权限,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.addPer(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("添加权限 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加权限 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改权限 updatePer
     **/

    public BaseRespose updatePer(BaseRequest baseRequest) {
        log.info("修改权限,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.updatePer(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("修改权限 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改权限 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除权限 deletePer
     **/
    public BaseRespose deletePer(BaseRequest baseRequest) {
        log.info("删除权限,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.deletePer(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("删除权限 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除权限 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工个人中心 selectStaffInfo
     **/
    public BaseRespose selectStaffInfo(BaseRequest baseRequest) {
        log.info("员工个人中心,参数为{}", JSON.toJSONString(baseRequest));
        BaseRespose baseRespose = new BaseRespose();
        try {
            baseRespose = userManager.selectStaffInfo(baseRequest);
        } catch (ServiceException e) {
            e.printStackTrace();
            log.error("员工个人中心 异常，异常编号{}，异常代码{},参数为{}", e.getcode(), e.getMessage(), JSON.toJSONString(baseRequest));
            baseRespose.setCode(e.getcode());
            baseRespose.setDisp(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("员工个人中心 错误，{},参数为{}", e, JSON.toJSONString(baseRequest));
            baseRespose.setCode(ServiceCodeEnum.FAIL.getCode());
            baseRespose.setDisp(ServiceCodeEnum.FAIL.getDesc());
        }
        return baseRespose;
    }


}
